package com.itmentu.controller;

import com.itmentu.config.DeadExchangeConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessagePostProcessor;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.AbstractJavaTypeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.awt.print.Book;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

/**
 * @Author：http://www.itmentu.com
 * @date： 2021/12/6 10:49
 * @Description：
 ***/
@Slf4j
@RestController
@RequestMapping(value = "/producer")
public class MessageProducer {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    /**
     * 死信队列发送消息
     */
    @GetMapping(value = "/dead/send")
    public String deadSend(String msg){
        //SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //this.rabbitTemplate.convertAndSend("dead_letter_exchange", "dead", simpleDateFormat.format(new Date())+" itmentu发来的消息:"+msg, message -> {
        //    // 如果配置了 params.put("x-message-ttl", 5 * 1000); 那么这一句也可以省略,具体根据业务需要是声明 Queue 的时候就指定好延迟时间还是在发送自己控制时间
        //    message.getMessageProperties().setExpiration(1000 * 30 + "");
        //    return message;
        //});

        //发送消息
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        rabbitTemplate.convertAndSend("dead_letter_exchange", "dead", simpleDateFormat.format(new Date())+" itmentu发来的消息:"+msg);
        return "SUCCESS";
    }

    /**
     * 消息确认机制发送消息
     */
    @GetMapping(value = "/publisher/send")
    public String publisherSend(String message){
        /***
         * 消息发送
         * 发送的消息是message
         */
        rabbitTemplate.convertAndSend("direct_publisher_exchange","publisher",message);
        return "SUCCESS";
    }


    /**
     * 消息发送
     */
    @GetMapping(value = "/topic/send")
    public String topicSend(String message){
        /***
         * 消息发送
         * 发送的消息是message
         */
        rabbitTemplate.convertAndSend("topic_exchange","itmentu.pay",message);
        return "SUCCESS";
    }

    /**
     * 消息发送
     */
    @GetMapping(value = "/fanout/send")
    public String fanoutSend(String message){
        /***
         * 消息发送
         * 发送的消息是message
         */
        rabbitTemplate.convertAndSend("fanout_exchange",null,message);
        return "SUCCESS";
    }

    /**
     * 消息发送
     */
    @GetMapping(value = "/work/send")
    public String workSend(String message){
        /***
         * 消息发送
         * 发送的消息是message
         */
        rabbitTemplate.convertAndSend("work_queue",message);
        return "SUCCESS";
    }

    /**
     * 消息发送
     */
    @GetMapping(value = "/simple/send")
    public String simpleSend(String message){
        /***
         * 消息发送
         * 发送的消息是message
         */
        rabbitTemplate.convertAndSend("simple_queue",message);
        return "SUCCESS";
    }

    /**
     * 消息发送
     */
    @GetMapping(value = "/direct/send")
    public String directSend(String message){
        /***
         * 消息发送
         * 路由的key是itmentu.topic
         * 发送的消息是message
         */
        rabbitTemplate.convertAndSend("direct_exchange","direct",message);
        return "SUCCESS";
    }
}
